﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ControlsLib.Filters;
using GestionePolveriLib.BL.FilterBags;
using GestionePolveriLib.Utilities;
using GestionePolveriLib.BL;

namespace GestionePolveri2.Filters
{
    public partial class AnagraficaPOSFilter : AnagraficaPOSFilterBase
    {
        public AnagraficaPOSFilter()
        {
            InitializeComponent();

            if (!this.DesignMode)
            {
                RefreshData();

                this.FromDate = DateTime.Now;
                this.ToDate = DateTime.Now;

                this.EnableDatesChooser = true;
            }
        }
        
        public void ShowOperatoriCombo(bool show)
        {
            this.label5.Visible = show;
            this.cmbOperatori.Visible = show;
        }

        protected override void OnResetFilter(EventArgs e)
        {
            base.OnResetFilter(e);

            FilterComboBoxHelper.Reset(this.cmbTurno);
            FilterComboBoxHelper.Reset(this.cmbImpianto);
            FilterComboBoxHelper.Reset(this.cmbOperatori);
            //this.cmbTurno.SelectedIndex = -1;
            //this.cmbImpianto.SelectedIndex = -1;
            //this.cmbOperatori.SelectedIndex = -1;
        }

        protected override void OnExecuteFilter(EventArgs e)
        {
            Turno turnoSelected = FilterComboBoxHelper.GetSelectedItem<Turno>(this.cmbTurno);
            Impianto impiantoSelected = FilterComboBoxHelper.GetSelectedItem<Impianto>(this.cmbImpianto);
            Operatore operatoreSelected = FilterComboBoxHelper.GetSelectedItem<Operatore>(this.cmbOperatori);

            FilterBag.Turno = (turnoSelected == null ? null : turnoSelected.Descrizione);
            FilterBag.Impianto = (impiantoSelected == null ? null : impiantoSelected.Nome);
            FilterBag.IDOperatore = (operatoreSelected == null ? Guid.Empty : operatoreSelected.ID);

            if (
                (this.cmbTurno.SelectedIndex == -1) &&
                (this.cmbImpianto.SelectedIndex == -1) &&
                (this.cmbOperatori.SelectedIndex == -1)
               )
            {
                OnResetFilter(e);
                this.OnFilterButtonClick();
            }
            else
            {
                base.OnExecuteFilter(e);
            }
        }

        public override void RefreshData()
        {
            LoadTurni();
            LoadImpianti();
            LoadOperatori();
        }

        private void LoadTurni()
        {
            FilterComboBoxHelper.SetupTurniComboBox(this.cmbTurno);
        }

        private void LoadImpianti()
        {
            FilterComboBoxHelper.SetupImpiantiComboBox(this.cmbImpianto);
        }

        private void LoadOperatori()
        {
            FilterComboBoxHelper.SetupOperatoriComboBox(this.cmbOperatori);
        }
    }

    public class AnagraficaPOSFilterBase : FilterContainerBase2<AnagraficaPOSFilterBag> { }
}
